const express = require('express');
const fs = require('fs');
const path = require('path');

const app = express();

app.use((req, res, next) => {
    console.log('服务器1启动了');
    next();
})

app.get('/students', (req, res) => {
    const students = [
        {id:'001',name:'张三',age:18},
        {id:'002',name:'李四',age:19},
        {id:'003',name:'王五',age:20}
    ]
    res.send(students)
})

app.get('/search/users', (erq, res) => {
    console.log('get参数：' + erq.query.q);
    
    setTimeout(() => {
        //获取文件
        const users = JSON.parse(fs.readFileSync(path.join(__dirname, '../', 'public', 'data', 'gathubUsers.json')));
        
        res.send(users)
    }, 3000);
})

app.listen(5000, (err) => {
    if(!err) console.log('服务启动成功！http://localhost:5000');
})